package delivery.order.service.task;


import delivery.order.service.constant.PathConstant;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
@EnableScheduling
public class ClearTempFileTask {

    private static final Logger logger = LoggerFactory.getLogger(ClearTempFileTask.class);

    // 清理临时文件
    @Scheduled(cron = "0 0 5 * * ?")//每天早上5点整执行
    private void clearFile() {
        File folder = new File(PathConstant.getOutputFolderPath());
        try {
            if (folder.exists() && folder.isDirectory()) {
                FileUtils.cleanDirectory(folder);
                logger.warn("文件夹已清空: {}", PathConstant.getOutputFolderPath());
            }
        } catch (Exception e) {
            logger.error("清空文件夹失败: {}", e.getMessage());
        }
    }
}
